/*************************************************************************
*																		 *
*				code by Miles Chen (v-mich) 2007.10.8					 *
*																		 *
*																		 *
*************************************************************************/

#pragma once

#include "commdef-int.h"

namespace System{namespace IO{namespace File{

	BOOL GetFullPath(IN LPCWSTR lpszRelativePath, OUT LPWSTR lpszFullPath, int cb)
	{
		WCHAR szDirectroy[MAX_CHAR];
		GetCurrentDirectoryW(MAX_CHAR, szDirectroy);
		wsprintfW(lpszFullPath, L"%ws\\%ws", szDirectroy, lpszRelativePath);
		return TRUE;
	}

	BOOL Exist(LPCWSTR lpszFullPath)
	{
		return Exist(lpszFullPath, FALSE);
	}

	BOOL Exist(LPCWSTR lpszFile, BOOL fRelativePath)
	{
		BOOL result;
		WIN32_FIND_DATA FindFileData;
		HANDLE hFind;

		if(fRelativePath)
		{
			WCHAR szFullPath[MAX_CHAR];
			GetFullPath(lpszFile, szFullPath, MAX_CHAR);
			lpszFile = szFullPath;
		}

		hFind = FindFirstFile(lpszFile, &FindFileData);
		result = (hFind != INVALID_HANDLE_VALUE);

		if(result)
		{
			FindClose(hFind);
		}

		return result;
	}

	HANDLE Create(LPCWSTR lpszFullPath)
	{
		return Create(lpszFullPath, FALSE);
	}

	HANDLE Create(LPCWSTR lpszFile, BOOL fRelativePath)
	{
		WCHAR szBufferPath[MAX_CHAR];

		if(fRelativePath)
		{
			GetFullPath(lpszFile, szBufferPath, MAX_CHAR);
		}
		else
		{
			String::CopyTo(szBufferPath, lpszFile);
		}

		return CreateFileW(
			szBufferPath,			   // file to open
			GENERIC_READ | GENERIC_WRITE,          // open for reading/writing
			FILE_SHARE_READ | FILE_SHARE_WRITE,    // share for reading/writing
			NULL,                  // default security
			CREATE_ALWAYS,         // create always
			FILE_ATTRIBUTE_NORMAL, // normal file
			NULL);                 // no attr. template
	}

	HANDLE Open(LPCWSTR lpszFullPath)
	{
		return Open(lpszFullPath, FALSE);
	}

	HANDLE Open(LPCWSTR lpszFile, BOOL fRelativePath)
	{
		WCHAR szBufferPath[MAX_CHAR];

		if(fRelativePath)
		{
			GetFullPath(lpszFile, szBufferPath, MAX_CHAR);
		}
		else
		{
			String::CopyTo(szBufferPath, lpszFile);
		}

		return CreateFileW(
			szBufferPath,			// file to open
			GENERIC_READ,          // open for reading
			FILE_SHARE_READ,       // share for reading
			NULL,                  // default security
			OPEN_EXISTING,         // open an existing file
			FILE_ATTRIBUTE_NORMAL, // normal file
			NULL);                 // no attr. template
	}

	BOOL Close(HANDLE hFile)
	{
		return CloseHandle(hFile);
	}

	BOOL VerifyUnicodeFormat(HANDLE hFile)
	{
		//UNICDE!!
		WORD wBOM = 0xFEFF;
		WORD buffer;
		DWORD dwRead;
		return ReadFile(hFile, &buffer, sizeof(WORD), &dwRead, NULL) && buffer == wBOM;
	}

	BOOL WriteUnicodeFormat(HANDLE hFile)
	{
		//UNICDE!!
		WORD wBOM = 0xFEFF;
		DWORD dwWritten;
		return WriteFile(hFile, &wBOM, sizeof(WORD), &dwWritten, NULL);
	}

	BOOL ReadLine(HANDLE hFile, LPWSTR lpszString)
	{
		WCHAR buffer;
		DWORD dwRead;
		LPWSTR p = lpszString;

		*p = L'\0';
		while(ReadFile(hFile, &buffer, sizeof(WCHAR), &dwRead, NULL) && dwRead && buffer != L'\n')
		{
			if(buffer != L'\r')
				*p++ = buffer;
		}
		*p = L'\0';

		if(*lpszString == L'\0')
			return FALSE;
		return TRUE;
	}

	BOOL WriteLine(HANDLE hFile, LPCWSTR lpszString)
	{
		DWORD dwWritten;
		BOOL result;

		result = WriteFile(hFile, lpszString, (DWORD)(sizeof(WCHAR) * System::String::Length(lpszString)), &dwWritten, NULL);
		result = WriteFile(hFile, L"\r\n", (DWORD)(sizeof(WCHAR) * 2), &dwWritten, NULL);

		return result;
	}

}}}